package com.jhy.javacase.io;



import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jhy
 * @ClassName FileInputOutputStreamCase.java
 * @Description FileInputStream和FileOutputStreamr入门案例
 * @createTime 2022年02月27日 13:46:00
 */
public class FileInputOutputStreamCase {

    public static void main(String[] args) throws IOException {

        //1.文件输入流
        FileInputStream inputStream = new FileInputStream("E:\\java项目\\Guava学习\\guava-case\\guava-case\\src\\main\\resources\\io\\sourceFile.txt");
        //存储读出来的字节
        List<Byte> bytes = new ArrayList<>();
        Byte val;
        //read()方法一次读取一个字节，返回值为该字节的值；当返回值为-1，表示读取结束
        while ((val = (byte) inputStream.read())!= -1){
            bytes.add(val);
        }

        //将读出来的字节转为字符串输出
        byte[] array = new byte[bytes.size()];
        for (int i = 0; i < bytes.size(); i++) {
            array[i] = bytes.get(i);
        }
        System.out.println(new String(array));
        inputStream.close();

        //2.文件输出流
        FileOutputStream outputStream = new FileOutputStream("E:\\java项目\\Guava学习\\guava-case\\guava-case\\src\\main\\resources\\io\\TargetFile.txt");
        outputStream.write("hello java".getBytes("UTF-8"));
        //flush刷新
        outputStream.flush();
        outputStream.close();
    }
}
